﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.LIB;

namespace HYElectricSystemSetting
{
	public partial class EnvironmentTemperatureForm : System.Windows.Forms.Form
	{
		public EnvironmentTemperatureForm()
		{
			this.InitializeComponent();
			this.btnDeleteItems.Image = Resource.GetImage("delete16");
		}

		private void EnvironmentTemperatureForm_Load(object sender, EventArgs e)
		{
			ToolTip toolTip = new ToolTip();
			toolTip.AutoPopDelay = 5000;
			toolTip.InitialDelay = 1000;
			toolTip.ReshowDelay = 50;
			toolTip.ShowAlways = true;
			toolTip.SetToolTip(this.btnAddItems, "添加");
			toolTip.SetToolTip(this.btnReName, "编辑");
			toolTip.SetToolTip(this.btnDeleteItems, "删除");
			this.AddComboxItem();
			if (this.cmbEvTemp.Items.Count > 0)
			{
				this.cmbEvTemp.SelectedIndex = 0;
			}
		}

		public void AddComboxItem()
		{
			this.cmbEvTemp.Items.Clear();
			if (ElectricSystemSettingData.WireTypeDatas == null)
			{
				return;
			}
			foreach (WireTypeData item in ElectricSystemSettingData.WireTypeDatas)
			{
				this.cmbEvTemp.Items.Add(item);
				this.cmbEvTemp.DisplayMember = "WireType";
			}
		}

		private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
			{
				this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
				this.dataGridView1.BeginEdit(true);
			}
		}

		private void toolStripButton1_Click(object sender, EventArgs e)
		{
			this.dataGridView1.Rows.Add();
		}

		private void toolStripButton2_Click(object sender, EventArgs e)
		{
			this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
		}

		private void cmbEvTemp_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.dataGridView1.Rows.Clear();
			WireTypeData wireTypeData = (WireTypeData)this.cmbEvTemp.SelectedItem;
			if (wireTypeData == null)
			{
				return;
			}
			this.dataGridView1.Rows.Clear();
			foreach (EnvTemperatureCorrectionFactorData envTemperatureCorrectionFactorData in ElectricSystemSettingData.EnvTemperatureCorrection)
			{
				if (envTemperatureCorrectionFactorData.WireType == wireTypeData.Id)
				{
					this.dataGridView1.Rows.Clear();
					Dictionary<int, double> correctionFactorItems = envTemperatureCorrectionFactorData.CorrectionFactorItems;
					if (correctionFactorItems.Count != 0)
					{
						this.dataGridView1.Rows.Add(correctionFactorItems.Count);
						int num = 0;
						int num2 = 0;
						using (Dictionary<int, double>.Enumerator enumerator2 = correctionFactorItems.GetEnumerator())
						{
							while (enumerator2.MoveNext())
							{
								KeyValuePair<int, double> keyValuePair = enumerator2.Current;
								this.dataGridView1.Rows[num2].ReadOnly = true;
								this.dataGridView1.Rows[num].Cells[0].Value = keyValuePair.Key;
								this.dataGridView1.Rows[num].Cells[1].Value = keyValuePair.Value;
								num++;
								num2++;
							}
							break;
						}
					}
				}
			}
			this.dataGridView1.Sort(this.dataGridView1.Columns[0], ListSortDirection.Ascending);
		}

		private void cmbEvTemp_MouseClick(object sender, MouseEventArgs e)
		{
		}

		public void SaveFormData()
		{
			bool flag = true;
			EnvironmentTemperatureForm.EnvIsEdit = true;
			WireTypeData wireTypeData = (WireTypeData)this.cmbEvTemp.SelectedItem;
			EnvTemperatureCorrectionFactorData envTemperatureCorrectionFactorData = new EnvTemperatureCorrectionFactorData();
			if (wireTypeData == null)
			{
				return;
			}
			foreach (EnvTemperatureCorrectionFactorData envTemperatureCorrectionFactorData2 in ElectricSystemSettingData.EnvTemperatureCorrection)
			{
				if (envTemperatureCorrectionFactorData2.WireType == wireTypeData.Id)
				{
					flag = false;
					Dictionary<int, double> dictionary = new Dictionary<int, double>();
					for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
					{
						if (this.dataGridView1.Rows[i].Cells[0].Value != null && this.dataGridView1.Rows[i].Cells[0].Value.ToString() != "" && !dictionary.ContainsKey(Convert.ToInt32(this.dataGridView1.Rows[i].Cells[0].Value)))
						{
							dictionary.Add(Convert.ToInt32(this.dataGridView1.Rows[i].Cells[0].Value), Convert.ToDouble(this.dataGridView1.Rows[i].Cells[1].Value));
						}
					}
					envTemperatureCorrectionFactorData2.CorrectionFactorItems = dictionary;
					break;
				}
			}
			if (flag)
			{
				envTemperatureCorrectionFactorData.WireType = wireTypeData.Id;
				Dictionary<int, double> dictionary2 = new Dictionary<int, double>();
				for (int j = 0; j < this.dataGridView1.Rows.Count; j++)
				{
					if (this.dataGridView1.Rows[j].Cells[0].Value != null && this.dataGridView1.Rows[j].Cells[0].Value.ToString() != "" && !dictionary2.ContainsKey(Convert.ToInt32(this.dataGridView1.Rows[j].Cells[0].Value)))
					{
						dictionary2.Add(Convert.ToInt32(this.dataGridView1.Rows[j].Cells[0].Value), Convert.ToDouble(this.dataGridView1.Rows[j].Cells[1].Value));
					}
				}
				envTemperatureCorrectionFactorData.CorrectionFactorItems = dictionary2;
				ElectricSystemSettingData.EnvTemperatureCorrection.Add(envTemperatureCorrectionFactorData);
			}
		}

		private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			this.SaveFormData();
		}

		private void btnAddItems_Click(object sender, EventArgs e)
		{
			if (ElectricSystemSettingData.DataGridViewAddRow(1, this.dataGridView1))
			{
				this.SaveFormData();
			}
		}

		private void btnDeleteItems_Click(object sender, EventArgs e)
		{
			if (this.dataGridView1.Rows.Count >= 1 && this.dataGridView1.SelectedRows.Count > 0)
			{
				this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
				this.SaveFormData();
			}
		}

		private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
		{
			this.dataGridView1.FirstDisplayedScrollingRowIndex = e.RowIndex;
		}

		private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			this.dgvTxt = (DataGridViewTextBoxEditingControl)e.Control;
			this.dgvTxt.SelectAll();
			this.dgvTxt.KeyPress += this.Cells_KeyPress;
		}

		private void Cells_KeyPress(object sender, KeyPressEventArgs e)
		{
			ElectricSystemSettingData.keyPressXS(e, this.dgvTxt);
		}

		private void btnReName_Click(object sender, EventArgs e)
		{
			if (ElectricSystemSettingData.DatagridviewEditRow(1, this.dataGridView1))
			{
				this.SaveFormData();
			}
		}

		public static bool EnvIsEdit;

		public DataGridViewTextBoxEditingControl dgvTxt;
	}
}
